﻿using MvvmCross.Core.ViewModels;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Sidvall.AuthorizationManager.UI.ViewModels
{
	public class DocumentationViewModel : Sidvall.UI.ViewModels.ViewModelBase
	{
		#region Public Members

		#region OverviewIsVisible

		private bool _OverviewIsVisible;
		public bool OverviewIsVisible
		{
			get
			{
				if (_PageType != null)
					return (_PageType.Value == PageType.Overview);
				return _OverviewIsVisible;
			}
			set
			{
				_OverviewIsVisible = value;
				RaisePropertyChanged("OverviewIsVisible");
			}
		}

		#endregion
		#region ServerIsVisible

		private bool _ServerIsVisible;
		public bool ServerIsVisible
		{
			get
			{
				if (_PageType != null)
					return (_PageType.Value == PageType.Server);
				return _ServerIsVisible;
			}
			set
			{
				_ServerIsVisible = value;
				RaisePropertyChanged("ServerIsVisible");
			}
		}

		#endregion
		#region HelpTextIsVisible

		private bool _HelpTextIsVisible;
		public bool HelpTextIsVisible
		{
			get
			{
				return _HelpTextIsVisible;
			}
			set
			{
				_HelpTextIsVisible = value;
				RaisePropertyChanged("HelpTextIsVisible");
			}
		}

		#endregion
		#region ServerScript

		private string _ServerScript;
		public string ServerScript
		{
			get
			{
				return _ServerScript;
			}
			set
			{
				_ServerScript = value;
				RaisePropertyChanged("ServerScript");
			}
		}

		#endregion
		#region HelpText

		private string _HelpText;
		public string HelpText
		{
			get
			{
				return _HelpText;
			}
			set
			{
				_HelpText = value;
				RaisePropertyChanged("HelpText");
			}
		}

		#endregion

		#region SelectHelpCommand

		public ICommand SelectHelpCommand
		{
			get
			{
				return new MvxCommand<string>(SelectHelp);
			}
		}

		#endregion
		#region SelectHelp

		public void SelectHelp(string id)
		{
			if (id == _CurrentId)
				this.HelpTextIsVisible = !this.HelpTextIsVisible;
			else
				this.HelpTextIsVisible = true;
			switch (id)
			{
				case "1":
					this.HelpText = "This is the tool you are using now";
					break;
				case "2":
					this.HelpText = "A database with tables that the Authorization Manager\r\nuses to store information";
					break;
				default:
					break;
			}
			_CurrentId = id;
		}

		#endregion
		#region SelectPageCommand

		public ICommand SelectPageCommand
		{
			get
			{
				return new MvxCommand<string>(SelectPage);
			}
		}

		#endregion
		#region SelectPage

		public void SelectPage(string id)
		{
			this.OverviewIsVisible = false;
			this.ServerIsVisible = false;
			switch (id)
			{
				case "1":
					this.OverviewIsVisible = true;
					break;
				case "2":
					this.ServerIsVisible = true;
					break;
				default:
					break;
			}
		}

		#endregion

		#region StartAsync

		protected override async Task StartAsync()
		{
			_PageType = null;
			SelectPage("1");

			this.ServerScript = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(@"Data\AuthorizationManager.sql");
		}

		#endregion

		#endregion
		#region Private Members

		private string _CurrentId;
		private PageType? _PageType = PageType.Server;
		private enum PageType
		{
			Overview,
			Server,
		}

		#endregion
	}
}
